import 'dart:ui';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/data/user_data_entity.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/home/home_share_page.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:screenshot/screenshot.dart';

class MinEWMPage extends StatefulWidget {
  final UserDataEntity entity;
  const MinEWMPage({Key? key, required this.entity}) : super(key: key);

  @override
  _MinEWMPageState createState() => _MinEWMPageState();
}

class _MinEWMPageState extends State<MinEWMPage> {

  ScreenshotController _screenshotController = ScreenshotController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgColor,
      body: Container(
        width: 1.sw,
        height: 1.sh,
        child: Stack(
          children: [
            Container(
              child: Image.asset('assets/min/min_top_bg.png',width: 1.sw,fit: BoxFit.fitWidth,),
            ),
            Positioned(child: Container(
              child: Row(
                children: [
                  GestureDetector(
                    onTap: (){
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      width: 100.w,
                      height: 80.w,
                      alignment: Alignment.centerLeft,
                      child: Icon(Icons.arrow_back_ios,color: Colors.white,size: 40.w,),
                    ),
                  ),
                  Expanded(child: Container(
                    height: 80.w,
                    alignment: Alignment.center,
                    child: Text('二维码名片',style: TextStyle(
                        color: Colors.white,
                        fontSize: 36.sp,
                        fontWeight: FontWeight.w600
                    ),),
                  )),
                  Container(
                    width: 100.w,
                    height: 80.w,
                  ),
                ],
              ),
            ),top: ScreenUtil().statusBarHeight+10.w,left: 30.w,right: 30.w,),
            Positioned(
              child: Container(
                child: Column(
                  children: [
                    Screenshot(
                      controller: _screenshotController,
                      child: Container(
                        height: 950.w,
                        width: 700.w,
                        padding: EdgeInsets.all(30.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20.w),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black.withOpacity(0.2),
                              blurRadius: 20,
                            ),
                          ],
                        ),
                        child: Column(
                          children: [
                            Container(
                              child: Row(
                                children: [
                                  ClipRRect(
                                    child:ASRCacheImage(image: widget.entity.img??'assets/icon/p_head.png',width: 110.w,height: 110.w,fit: BoxFit.fitWidth,),
                                    borderRadius: BorderRadius.circular(110.w),
                                  ),
                                  Expanded(child: Container(
                                    margin: EdgeInsets.symmetric(horizontal: 20.w),
                                    height: 110.w,
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        RichText(
                                          maxLines: 1,
                                          softWrap: true,
                                          text: TextSpan(
                                            text: widget.entity.name??'',
                                            style: TextStyle(
                                                fontSize: 30.sp,
                                                color: Colors.black,
                                                fontWeight: FontWeight.w600
                                            ),
                                          ),
                                          textAlign: TextAlign.left,
                                        ),
                                        Text('地址：${userInfo.addressRegin??''}${userInfo.addressDetail??''}',maxLines: 2,style:  TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 24.sp,
                                        ),)
                                      ],
                                    ),
                                  )),
                                  Container(
                                    child: Row(
                                      children: [
                                        Text('粮策号: ${widget.entity.grainid}',style: TextStyle(
                                            fontSize: 26.sp,
                                            color: Color(0xffEB6E30),
                                            fontWeight: FontWeight.w600
                                        ),),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),
                            Container(
                              height: 0.5,
                              margin: EdgeInsets.only(top: 30.w,bottom: 50.w),
                              color: Color(0xffE9E8E9),
                            ),
                            Container(
                              padding: EdgeInsets.symmetric(horizontal: 30.w),
                              alignment: Alignment.centerLeft,
                              child: Text('介绍：${widget.entity.profiles??'个人简介...'}',style: TextStyle(
                                  color: Color(0xff2F2F2F),
                                  fontSize: 34.sp,
                                  fontWeight: FontWeight.w500
                              ),),
                            ),
                            Container(
                              margin: EdgeInsets.symmetric(vertical: 60.w),
                              child: Container(
                                color: Colors.transparent,
                                // child: QrImage(
                                //   size: 320.w,
                                //   padding: EdgeInsets.zero,
                                //   data: '${BasVueurl}/#/?lcID=${widget.entity.id}',
                                // ),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.symmetric(vertical: 40.w),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Container(
                                    height: 2,
                                    width: 60.w,
                                    color: Color(0xffE9E8E9),
                                  ),
                                  SizedBox(width: 30.w,),
                                  Text('打开粮策或微信扫一扫关注我',style: TextStyle(
                                      fontSize: 24.sp,
                                      color: Color(0xff5A5A5A),
                                      fontWeight: FontWeight.w600
                                  ),),
                                  SizedBox(width: 30.w,),
                                  Container(
                                    height: 2,
                                    width: 60.w,
                                    color: Color(0xffE9E8E9),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 50.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          GestureDetector(
                            onTap: ()async{
                              final dir = await getTemporaryDirectory();
                              String path = dir.path;
                              String fileName =
                                  DateTime.now().microsecondsSinceEpoch.toString() + '.jpg';
                              await _screenshotController.captureAndSave(
                                  path, //set path where screenshot will be saved
                                  fileName: fileName,
                                  pixelRatio: window.devicePixelRatio ?? 2)
                                  .then((value) {
                                ImageGallerySaverPlus.saveFile('$path/$fileName').then((value) {
                                  showToast('保存成功~');
                                });
                              });
                            },
                            child: Image.asset('assets/min/min_save_bg.png',width: 320.w,),
                          ),
                          SizedBox(width: 30.w,),
                          lcGestureDetector(
                            isLogin:true,
                            onTap: (){
                              SmartDialog.show(
                                alignment: Alignment.bottomCenter,
                                clickMaskDismiss: true,
                                maskColor: Color.fromRGBO(0, 0, 0, 0.5), builder: (BuildContext context) {
                                  return HomeSharepage(
                                    contentType: 5,
                                    webURL: '${BasVueurl}/#/?lcID=${widget.entity.id}',
                                  );
                              },
                              );
                            },
                            child: Image.asset('assets/min/min_share_bg.png',width: 320.w,),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
              top: ScreenUtil().statusBarHeight+120.w,left: 30.w,right: 30.w,
            )
          ],
        ),
      ),
    );
  }
}
